package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.controller.OmcCMDisinfectionController;
import com.csun.cmny.provider.dao.OmcServiceFirstLevelMapper;
import com.csun.cmny.provider.dao.OmcServiceSecondLevelMapper;
import com.csun.cmny.provider.dto.ServiceQueryDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcMerchandise;
import com.csun.cmny.provider.model.domain.OmcServiceFirstLevel;
import com.csun.cmny.provider.model.domain.OmcServiceSecondLevel;
import com.csun.cmny.provider.model.vo.ServiceFirstLevelVO;
import com.csun.cmny.provider.model.vo.ServiceThirdLevelVO;
import com.csun.cmny.provider.service.OmcMerchandiseService;
import com.csun.cmny.provider.service.OmcServiceFirstLevelService;
import com.csun.cmny.provider.service.OmcServiceSecondLevelService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.provider.utils.AppUtil;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcServiceFirstLevelServiceImpl extends BaseService<OmcServiceFirstLevel> implements OmcServiceFirstLevelService {

    @Resource
    private OmcServiceFirstLevelMapper omcServiceFirstLevelMapper;
    @Resource
    private UacRpcService uacRpcService;
    @Resource
    private OmcServiceSecondLevelService serviceSecondLevelService;
    @Resource
    private OmcServiceSecondLevelMapper omcServiceSecondLevelMapper;


    @Override
    public Integer saveServiceFirstLevel(OmcServiceFirstLevel omcServiceFirstLevel, LoginAuthDto loginAuthDto, String type) {

       /* if(PublicUtil.isEmpty(omcServiceFirstLevel.getGroupId())){
            omcServiceFirstLevel.setGroupId(loginAuthDto.getGroupId());
        }*/

        //校验名称
        checkNameExsit(omcServiceFirstLevel, loginAuthDto);

        if ("update".equals(type)) {
            // 验证是否存在子项
            checkIsSon(omcServiceFirstLevel);

        }

        if (PublicUtil.isEmpty(omcServiceFirstLevel.getId())) {
            return omcServiceFirstLevelMapper.insertSelective(omcServiceFirstLevel);
        } else {
            Integer integer = uacRpcService.checkServiceFirstLevelUsed(omcServiceFirstLevel.getId());
            if (integer > 0) {
                throw new OmcBizException(ErrorCodeEnum.OMC10080019);
            }
            return omcServiceFirstLevelMapper.updateByPrimaryKeySelective(omcServiceFirstLevel);
        }
    }

    @Override
    public List<ServiceFirstLevelVO> listService(ServiceQueryDto serviceQueryDto) {
        return omcServiceFirstLevelMapper.listService(serviceQueryDto);
    }

    @Override
    public int deleteFirstLevel(Long firstLevelId) {

        OmcServiceFirstLevel serviceFirstLevelQuery = selectByKey(firstLevelId);

        if (PublicUtil.isEmpty(serviceFirstLevelQuery)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10080033);
        }
        if (PublicUtil.isNotEmpty(serviceFirstLevelQuery.getRemark())) {
            if (serviceFirstLevelQuery.getRemark().equals("default")) {
                throw new OmcBizException(ErrorCodeEnum.OMC10080010);
            }
        }

        //校验是否绑定有二级服务类别
        OmcServiceSecondLevel omcServiceSecondLevel = new OmcServiceSecondLevel();
        omcServiceSecondLevel.setFirstLevelId(firstLevelId);
        List<OmcServiceSecondLevel> select = serviceSecondLevelService.select(omcServiceSecondLevel);
        if (PublicUtil.isNotEmpty(select)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10080032);
        }


        Integer count = uacRpcService.checkServiceFirstLevelUsed(firstLevelId);
        if (count > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10080018);
        }


        return omcServiceFirstLevelMapper.deleteByPrimaryKey(firstLevelId);
    }

    @Override
    public PageInfo listPage(ServiceQueryDto serviceQueryDto) {

        PageHelper.startPage(serviceQueryDto.getPageNum(), serviceQueryDto.getPageSize());
        List<ServiceFirstLevelVO> voList = omcServiceFirstLevelMapper.listPage(serviceQueryDto);
        PageInfo<ServiceFirstLevelVO> objectPageInfo = new PageInfo<>(voList);

        if (objectPageInfo.getTotal() == 0) {
            List<OmcServiceFirstLevel> omcServiceFirstLevelList = Lists.newArrayList(
                    OmcServiceFirstLevel.builder()
                            .name("助餐")
                            .groupId(serviceQueryDto.getGroupId())
                            .remark("default")
                            .build(),
                    OmcServiceFirstLevel.builder()
                            .name("助医")
                            .groupId(serviceQueryDto.getGroupId())
                            .remark("default")
                            .build(),
                    OmcServiceFirstLevel.builder()
                            .name("助洁")
                            .groupId(serviceQueryDto.getGroupId())
                            .remark("default")
                            .build(),
                    OmcServiceFirstLevel.builder()
                            .name("远程看护")
                            .groupId(serviceQueryDto.getGroupId())
                            .remark("default")
                            .build()
            );

            int i = batchSave(omcServiceFirstLevelList);

            omcServiceFirstLevelList.forEach(
                    service -> {
                        ServiceFirstLevelVO vo = new ServiceFirstLevelVO();
                        BeanUtils.copyProperties(service, vo);
                        voList.add(vo);
                    }
            );
        }

        return objectPageInfo;

    }

    @Override
    public List<String> selectServiceFirstLevel(List<Long> firstLevelIds) {
        return omcServiceFirstLevelMapper.selectServiceFirstLevel(firstLevelIds);
    }

    private void checkNameExsit(OmcServiceFirstLevel omcServiceFirstLevel, LoginAuthDto loginAuthDto) {
        Example example = new Example(OmcServiceFirstLevel.class);
        Example.Criteria criteria = example.createCriteria();
//        if (PublicUtil.isNotEmpty(omcServiceFirstLevel.getId())) {
//            criteria.andNotEqualTo("id", omcServiceFirstLevel.getId());
//        }

        if (!AppUtil.isNullOrEmpty(loginAuthDto.getGroupId().toString())) {
            criteria.andEqualTo("groupId", loginAuthDto.getGroupId());
        }

        //criteria.andEqualTo("groupId",omcServiceFirstLevel.getGroupId());
        criteria.andEqualTo("name", omcServiceFirstLevel.getName());
        int i = selectCountByExample(example);
        if (i > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10080004);
        }
    }

    private void checkIsSon(OmcServiceFirstLevel omcServiceFirstLevel) {
        Example example = new Example(OmcServiceSecondLevel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("firstLevelId", omcServiceFirstLevel.getId());
        List<OmcServiceSecondLevel> omcServiceSecondLevels = omcServiceSecondLevelMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(omcServiceSecondLevels)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10080005);
        }


    }


}
